package com.wshoto.ocean.service.api;

import com.wshoto.framework.common.annotation.AuditLog;
import com.wshoto.framework.common.base.ResultUtils;
import com.wshoto.ocean.service.api.service.AppService;
import com.wshoto.ocean.service.common.bo.AppInfoBO;
import com.wshoto.ocean.service.common.request.AppRegisterReq;
import com.wshoto.ocean.service.common.request.AppReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 示例Controller
 * Controller命名规范遵循大驼峰式命名，命名规则为Api+业务名称+Controller
 * Controller访问映射路径为：/api+/业务名+c(c可以不加，该项纯属个人个性化定义)
 * Controller层只承载入参校验及接口暴漏及数据返回，不承载任何业务逻辑
 * 不必在Controller捕获任何异常，所有异常均通过全局异常拦截捕获并返回相应的状态码及错误消息,包括参数校验异常也不必捕获处理，均会通过全局异常拦截捕获处理
 * 建议添加@CrossOrigin注解，防止跨域问题
 *
 * @author Lee
 */
@Api("应用注册")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/appc")
public class ApiAppController {

    /**
     * 业务逻辑层依赖注入，bean注入统一使用Resource注入，如有需要可以使用getter/setter或构造器注入,不推荐使用@Autowired注入
     */
    @Resource
    private AppService appService;

    /**
     * 示例API访问接口
     * 多参数有返回数据示例接口
     * 返回值类型统一为Sting类型，返回值调用ResultUtils.success(obj).toString()方法进行封装
     * 参数接收方式为Json请求接收，@RequestBody
     *
     * @return
     * @Validated校验参数，校验结果会通过全局异常拦截返回,不必做任何处理
     */
    @ApiOperation(value = "获取应用列表", responseContainer = "List", response = AppInfoBO.class)
    @PostMapping("/getApps")
    public String getApps(@RequestBody @Validated AppReq appReq, HttpServletRequest request) {
        return ResultUtils.success(appService.getApps(appReq)).toString();
    }

    /**
     * 示例API访问接口
     * 多参数无返回数据示例接口
     * 执行完业务逻辑层代码直接返回ResultUtils.success().toString()
     * 参数接收方式为Json请求接收，@RequestBody
     *
     * @return
     * @Validated校验参数，校验结果会通过全局异常拦截返回,不必做任何处理
     */
    @AuditLog(type = AuditLog.OperationType.SAVE, operation = "'注册了应用'+#appRegisterReq.appName")
    @ApiOperation("注册应用")
    @PutMapping("/appRegister")
    public String appRegister(@RequestBody @Validated AppRegisterReq appRegisterReq, HttpServletRequest request) {
        appService.appRegister(appRegisterReq);
        return ResultUtils.success().toString();
    }

    /**
     * 示例API访问接口
     * 单参数有返回数据示例接口
     * 返回值类型统一为Sting类型，返回值调用ResultUtils.success(obj).toString()方法进行封装
     * 参数接收方式为路径接收，Restfull风格url
     *
     * @return
     */
    @ApiOperation(value = "通过应用ID获取用户", response = AppInfoBO.class)
    @GetMapping("/getAppById/{appId}")
    public String getAppById(@PathVariable Long appId, HttpServletRequest request) {
        return ResultUtils.success(appService.getAppById(appId)).toString();
    }

    /**
     * 示例API访问接口
     * 单参数无返回数据示例接口
     * 执行完业务逻辑层代码直接返回ResultUtils.success().toString()
     * 参数接收方式为路径接收，Restfull风格url
     *
     * @return
     */
    @AuditLog(type = AuditLog.OperationType.DELETE, operation = "'删除了应用'+#appId")
    @ApiOperation("删除应用")
    @DeleteMapping("/deleteApp/{appId}")
    public String deleteApp(@PathVariable Long appId, HttpServletRequest request) {
        appService.deleteApp(appId);
        return ResultUtils.success().toString();
    }

}
